package io.homework;

import java.io.*;

/**
 * 使用缓冲流完成文件复制:BufferedInputStream和BufferedOutputStream
 * 提高读写速度(通常缓冲流是最终连接在低级流上的流)
 */
public class BufferedCopyDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("test.jpg");
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream("test2.jpg");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        long start = System.currentTimeMillis();
        int d;
        while((d=bis.read())!=-1){
            bos.write(d);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完成，耗时："+(end-start)+"ms");

        bis.close();
        bos.close();
        /*
        FileInputStream fis = new FileInputStream("test.jpg");
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream("test2.jpg");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        long start = System.currentTimeMillis();
        int d;
        while((d=bis.read())!=-1){
            bos.write(d);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完成，耗时："+(end-start)+"ms");

        bis.close();
        bos.close();
         */

    }
}
